package com.kpijavatraining.module;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.xml.sax.SAXException;

import com.kpijavatraining.module.io.ConsolePrinter;
import com.kpijavatraining.module.io.ConsoleReader;
import com.kpijavatraining.module.utils.Authorizer;
import com.kpijavatraining.module.utils.Registrator;
import com.kpijavatraining.module2.menu.AdminMenu;
import com.kpijavatraining.module2.menu.UserMenu;

public class School {

	private ConsoleReader consoleReader;
	private ConsolePrinter consolePrinter;
	
	public School(){
		consoleReader = new ConsoleReader();
		consolePrinter = new ConsolePrinter();
	}
	
	public void run() throws IOException, ClassNotFoundException, CloneNotSupportedException, SAXException, TransformerFactoryConfigurationError, TransformerException, ParserConfigurationException{
		loop:while(true){	
			consolePrinter.println("1 - registration");
            consolePrinter.println("2 - authorization");
            consolePrinter.println("3 - exit");
			switch(consoleReader.readLn()){
			case "1" : register(); break;
			case "2" : logIn(); break; 
			case "3" : break loop;
//			default  : consolePrinter.println("����� ���������");
			}
		}	
	}

	private void register() throws FileNotFoundException, IOException, TransformerFactoryConfigurationError, TransformerException, ParserConfigurationException, SAXException{
//		new Registrator().register();
	}
	
	private void logIn() throws IOException, ClassNotFoundException, CloneNotSupportedException, SAXException, ParserConfigurationException{
		Authorizer authorizer = new Authorizer();
		boolean isAuthorized = authorizer.authorization();
		if(isAuthorized){
			String group = authorizer.getUser().getGroup();
			switch(group){
			case "user" : new UserMenu().runMenu(authorizer.getUser());break;
			case "admin": new AdminMenu().runMenu();break;
			}
		}
	}
	
	public void close(){
		consoleReader.close();
	}
}
